package com.ethannhu.todaynews.component.content

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.ethannhu.superui.date.SuperDateUtil
import com.ethannhu.superui.decoration.GridDividerItemDecoration
import com.ethannhu.todaynews.R
import com.ethannhu.todaynews.databinding.ItemContentBinding
import com.ethannhu.todaynews.util.ImageUtil
import com.ixuea.superui.util.DensityUtil
import org.apache.commons.collections4.CollectionUtils
import org.apache.commons.lang3.StringUtils


class ContentAdapter(val viewModel: ContentViewModel) : BaseQuickAdapter<Content, ContentAdapter.ViewHolder>() {

    /**
     * 绑定数据
     */
    override fun onBindViewHolder(holder: ViewHolder, position: Int, item: Content?) {
        holder.bindData(item!!)
        holder.itemView.setOnClickListener {
            viewModel.itemClick(item)
        }

    }

    /**
     * 创建 VH
     */
    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(ItemContentBinding.inflate(LayoutInflater.from(context), parent, false))
    }

    // VH
    inner class ViewHolder(val binding: ItemContentBinding) : RecyclerView.ViewHolder(binding.root) {
        //显示数据
        fun bindData(item: Content) {
            if (StringUtils.isNotBlank(item.title)) {
                binding.content.text = item.title
            } else {
                binding.content.text = item.content
            }

            //昵称
            binding.nickname.text = item.user!!.nickname
            //评论数
            binding.commentsCount.text = binding.commentsCount.context.getString(
                R.string.comments_count,
                item.commentsCount
            )

            //发表时间多久前
            binding.date.text = SuperDateUtil.commonFormat(item.createdAt)

            //视频播放图
            binding.videoContainer.visibility = View.GONE
            binding.list.visibility = View.GONE

            if (StringUtils.isNotBlank(item.uri)) {
                binding.videoContainer.visibility = View.VISIBLE
                //显示图片 涉及缓存 使用 Glide
                ImageUtil.show(binding.icon, item.icons?.get(0))

                //视频时长
                binding.duration.text = SuperDateUtil.s2ms(item.duration)
            } else if (CollectionUtils.isNotEmpty(item.icons)) {
                //多张图片
                binding.list.visibility = View.VISIBLE
                //动态计算显示列数
                var spanCount = 2
                if (item.icons!!.size >= 3) {
                    spanCount = 3
                }

                //设置布局管理器
                val layoutManager = GridLayoutManager(binding.list.context, spanCount)
                binding.list.layoutManager = layoutManager

                //分割线
                if (binding.list.itemDecorationCount > 0) {
                    binding.list.removeItemDecorationAt(0)
                }

                //设置网格之间的空隙分割
                val itemDeclaration = GridDividerItemDecoration(
                    binding.list.context,
                    DensityUtil.dip2px(binding.list.context, 5f).toInt(),
                )
                binding.list.addItemDecoration(itemDeclaration)

                val adapter = ImageAdapter()
                adapter.setOnItemClickListener { adapter, view, position ->
                    viewModel.previewMedias(binding.list, item.icons!!, position)
                }
                binding.list.adapter = adapter
                adapter.submitList(item.icons!!)
            }

        }


    }
}
